<?php
/**
 * Created by PhpStorm.
 * User: 王杰
 * Date: 2017/1/4
 * Time: 9:30
 */
namespace Mall\Model;

use Think\Model;

class GoodsModel extends Model {

    protected $tableName = 'mall_goods';

    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT)
    );

    public function getGoods($id)
    {
        $goods = S('goods_' . $id);
        $check_empty = empty($goods);
        if ($check_empty) {
            $goods = $this->where(array('id' => $id))->find();
            if (!$goods) {
                return null;
            }
            S('goods_' . $id, $goods);
        }
        return $goods;
    }


    //获得所有商品的id
    public function getAllGoods(){
        $ids= $this->where('status!=-1')->field('id')->select();
        return $ids;
    }

    public function getAllGoodsByPage(){
        $ids= $this->where('status=1')->field('id')->limit(5)->select();
        return $ids;
    }

    public function getHotGoods()
    {
        $ids = S('mall_goods_list');
        if (empty($ids)) {
            $ids = $this->order('update_time desc')->where(array('status'=>array('neq',-1),'is_hot'=>1))->field('id')->limit(10)->select();
            S('mall_goods_list',$ids);
        }
        return $ids;
    }

    public function getCateGoods($type = '')
    {
        $ids = $this->where(array('cate' => $type, 'status' => 1))->order('update_time desc')->field('id')->select();
        return $ids;
    }

    public function getSortGoods($type='',$sort='new',$page,$price){
        if($type){
            if($sort=='price'){
                if($price){
                    $ids = $this->where(array('cate' => $type, 'status' => 1))->order('price desc')->page($page,10)->field('id')->select();
                }else{
                    $ids = $this->where(array('cate' => $type, 'status' => 1))->order('price ')->page($page,10)->field('id')->select();
                }
            }elseif ($sort=='sales'){
                $ids = $this->where(array('cate' => $type, 'status' => 1))->order('sales ')->page($page,10)->field('id')->select();
            }else{
                $ids = $this->where(array('cate' => $type, 'status' => 1))->order('update_time desc')->page($page,10)->field('id')->select();
            }
        }else{
            $ids = $this->where(array('status' => 1))->order('update_time desc')->page($page,10)->field('id')->select();
            if($sort=='price'){
                if($price){
                    $ids = $this->where(array('status' => 1))->order('price desc')->page($page,10)->field('id')->select();
                }else{
                    $ids = $this->where(array('status' => 1))->order('price ')->page($page,10)->field('id')->select();
                }
            }elseif ($sort=='sales'){
                $ids = $this->where(array('status' => 1))->order('sales ')->page($page,10)->field('id')->select();
            }else{
                $ids = $this->where(array('status' => 1))->order('update_time desc')->page($page,10)->field('id')->select();
            }
        }
        return $ids;
    }

    public function editData($data)
    {
        //检测show是否为空
        if (!mb_strlen($data['show'], 'utf-8')) {
            //msubstr字符串截取
            $data['show'] = msubstr(text($data['content']), 0, 200);
        }
        $detail['content'] = $data['content'];
        if ($data['id']) {
            $data['update_time'] = time();
            $res = $this->save($data);
            $detail['goods_id'] = $data['id'];
        } else {
            $data['create_time'] = $data['update_time'] = time();
            $res = $this->add($data);
            //action_log行为标识,触发行为的模型名,触发行为的记录id,执行行为的用户id ; is_login检测用户是否登入
            $detail['goods_id'] = $res;
        }
        if ($res) {
            D('Mall/GoodsArticle')->editData($detail);
        }
        return $res;
    }
}